﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Controls.Primitives;
using ESRI.ArcGIS.Client;
using SLMapApp.ServiceReference2;
namespace SLMapApp
{
    public partial class MessageSend : UserControl
    {
       

        public string _placeName = string.Empty; //监测点名称   
        public string _UserTel = string.Empty; //地区编号   
        string _monitorID = string.Empty; //监测点ID   
        string _year = string.Empty; //年份   
        string _issueNum = string.Empty; //期号   
        string _cycleID = string.Empty; //生育周期ID   
        string _seedingTypeID = string.Empty; //苗情等级ID   

        private Point _location;
        private bool _isShowing;
        private Popup _popup;
        private Grid _grid;
        private Canvas _canvas;
        private FrameworkElement _content;
        ServiceClient proxy = new ServiceClient();//为了方便读取配置文件 统一用一个服务代理对象
        public MessageSend(string name, string tellphone)
        {
            InitializeComponent();
            this._placeName = name;
            this.cunName.Text = name;
            this._UserTel = tellphone;
            this.telnumber.Text = tellphone;
        }  

        //初始化并显示弹出窗体.公共方法在显示菜单项时调用   
        public void Show(Point location)
        {
            if (_isShowing)
            throw new InvalidOperationException();
            _isShowing = true;
            _location = location;
            ConstructPopup();
            _popup.IsOpen = true;
        }

        //关闭弹出窗体   
        public void Close()
        {
            _isShowing = false;
            if (_popup != null)
            {
                _popup.IsOpen = false;
            }
        }

        //Default behavior for OnClickOutside() is to close the context menu when there is a mouse click event outside the context menu   
        protected virtual void OnClickOutside()
        {
            Close();
        }

        // 用Grid来布局，初始化弹出窗体   
        //在Grid里面添加一个Canvas，用来监测菜单项外面的鼠标点击事件   
        private void ConstructPopup()
        {
            if (_popup != null)
                return;
            _popup = new Popup();
            _grid = new Grid();
            _popup.Child = _grid;
            _canvas = new Canvas();
            _canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
            _canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };
            _canvas.Background = new SolidColorBrush(Colors.Transparent);
            _grid.Children.Add(_canvas);
            _content = this;
            _content.HorizontalAlignment = HorizontalAlignment.Left;
            _content.VerticalAlignment = VerticalAlignment.Top;
            _content.Margin = new Thickness(_location.X, _location.Y, 0, 0);
            _grid.Children.Add(_content);
            UpdateSize();
        }

        /// <summary>   
        /// 更新大小   
        /// </summary>   
        private void UpdateSize()
        {
            _grid.Width = Application.Current.Host.Content.ActualWidth;
            _grid.Height = Application.Current.Host.Content.ActualHeight;
            if (_canvas != null)
            {
                _canvas.Width = _grid.Width;
                _canvas.Height = _grid.Height;
            }
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SendBtn_Click(object sender, RoutedEventArgs e)
        {
            //WebServiceForMapSoapClient wsc = new WebServiceForMapSoapClient();
            proxy.SendMessageCompleted += new System.EventHandler<SendMessageCompletedEventArgs>(wsc_SendMessageCompleted);
            string equipno = this.telnumber.Text.ToString().Trim();
            string msg = (this.SendTime.SelectedItem as ComboBoxItem).Content.ToString() + this.ContentTBox.Text.ToString().Trim();
            proxy.SendMessageAsync(equipno, msg);
        }
        private void wsc_SendMessageCompleted(object sender, SendMessageCompletedEventArgs e)
        {
            if (e.Result == 0)
            {
                MessageBox.Show("信息发送成功");
            }
            else
            {
                MessageBox.Show("信息发送失败");
            }

        }
    }
}
